<template>
  <div class="synchronous">
    <el-dialog
      v-loading="loading"
      :title="title"
      :visible.sync="open1"
      width="800px"
      append-to-body
      :before-close="close"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" class="demo-ruleForm">
        <el-form-item label="分类名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="分类图片" prop="picter">
          <!-- <el-input v-model="form.picter" type="file" /> -->
          <el-upload
            class="upload-demo"
            action="http://192.168.0.116:8000/upload/image"
            :on-success="handleSuccess"
            :limit="1"
            :headers="{ Authorization: 'Bearer ' + token }"
            :data="{ extra: 'something' }"
            :before-upload="beforeUpload"
            name="image"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
          <!-- 显示上传的图片 -->
          <img v-if="imageUrl" :src="imageUrl" style="width: 90px;height: 90px;">
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button type="primary" @click="synchronous()">一键同步</el-button>
        <el-button type="primary" @click="close()">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRemote, getSync } from '@/api/product'

export default {
  name: 'Synchronous',
  props: {
    open1: {
      type: Boolean
    },
    title: {
      type: String,
      default: '分类同步'
    }
  },
  data() {
    return {
      loading: true,
      imageUrl: '',
      token: '',
      form: {
        name: '',
        picter: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入库位名称', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    open(val) {
      if (val) {
        // this.getList()
      }
    }
  },
  created() {
    this.token = this.getCookie('Admin-Token')
  },
  methods: {
    getCookie(cname) {
      var name = cname + '='
      var decodedCookie = decodeURIComponent(document.cookie)
      var ca = decodedCookie.split(';')
      for (var i = 0; i < ca.length; i++) {
        var c = ca[i]
        while (c.charAt(0) == ' ') {
          c = c.substring(1)
        }
        if (c.indexOf(name) == 0) {
          return c.substring(name.length, c.length)
        }
      }
      return ''
    },
    // 上传前的验证
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt500K = file.size / 1024 < 500
      if (!isJPG) {
        this.$message.error('只能上传jpg/png文件!')
      }
      if (!isLt500K) {
        this.$message.error('上传图片大小不能超过500kb!')
      }
      return isJPG && isLt500K
    },
    // 上传成功后的处理
    handleSuccess(response, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    synchronous() {
      this.loading = true
      getSync().then(res => {
        if (res.code === 10000) {
          this.loading = false
          this.$message({
            message: '同步成功',
            type: 'success'
          })
          this.$emit('update:open', false)
        }
      })
    },
    handleNodeClick(data) {
      // console.log(data)
    },
    getList() {
      this.loading = true
      getRemote().then(res => {
        if (res.code === 10000) {
          this.data = res.data
          this.loading = false
        }
      })
    },
    close() {
      this.$emit('update:open1', false)
    }
  }
}
</script>
